# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem     
  
  # If you want "remember me" functionality, add this before_filter to Application Controller
  before_filter :login_from_cookie

  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_grantworks_session_id'   
  
  # Specify the site-wide layout.
  layout 'pisd'
  
  def check_authorization
    user = User.find(session[:user])
    unless user.role.detect{|role|
      role.rights.detect{|right|
      right.action == action_name && right.controller == self.class.controller_path
      }
    }
    flash[:notice] = "You are not authorized to view the page you requested"
    request.env["HTTP_REFERER" ] ? (redirect_to :back) : (redirect_to home_url)
    return false
    end
  end
  
  
  
  #sets application headers to handle unicode
  def set_charset
    content_type = headers["Content-Type"] || "text/html" 
    if /^text\//.match(content_type)
      headers["Content-Type"] = "#{content_type}; charset=utf-8" 
    end
  end
end

